package com.example.dynamicplan;

public class ZeroOneKnapSack {
    public int ZeroOneKnapSackDP(int n, int c, int[] wgt, int[] val) {
        int[][] dp = new int[n + 1][c + 1];
        for (int i = 0; i < n; i++) {
            dp[i][0] = 0;
        }
        for (int j = 0; j < c; j++) {
            dp[0][j] = 0;
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < c; j++) {
                if (j - wgt[i - 1] < 0) {
                    dp[i][j] = dp[i - 1][j];
                }
                dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - wgt[i - 1]] + val[i - 1]);
            }
        }
        return dp[n][c];
    }

    public int ALLKnapSackDP(int n, int c, int[] wgt, int[] val) {
        int[][] dp = new int[n + 1][c + 1];
        for (int i = 0; i < n; i++) {
            dp[i][0] = 0;
        }
        for (int j = 0; j < c; j++) {
            dp[0][j] = 0;
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < c; j++) {
                if (j - wgt[i - 1] < 0) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - wgt[i - 1]] + val[i - 1]);
                }
            }
        }
        return dp[n][c];
    }
}
